package com.gc.controller.authServlet;

import com.gc.entity.BookSeller;
import com.gc.entity.Manager;
import com.gc.entity.Student;
import com.gc.entity.Teacher;
import com.gc.repository.BookSellerRepository;
import com.gc.repository.ManagerRepository;
import com.gc.repository.StudentRepository;
import com.gc.repository.TeacherRepository;
import com.gc.repository.impl.BookSellerRepositoryImpl;
import com.gc.repository.impl.ManagerRepositoryImpl;
import com.gc.repository.impl.StudentRepositoryImpl;
import com.gc.repository.impl.TeacherRepositoryImpl;
import com.gc.service.LoginService;
import com.gc.service.StudentService;
import com.gc.service.impl.LoginServiceImpl;
import com.gc.service.impl.StudentServiceImpl;
import com.gc.util.result.R;
import com.google.gson.Gson;
import org.apache.poi.ss.formula.functions.T;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "Login", value = "/login")
public class Login extends HttpServlet {
    LoginService loginService = new LoginServiceImpl();
    StudentRepository studentRepository = new StudentRepositoryImpl();
    BookSellerRepository bookSellerRepository = new BookSellerRepositoryImpl();
    ManagerRepository managerRepository = new ManagerRepositoryImpl();
    TeacherRepository teacherRepository = new TeacherRepositoryImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(request.getParameter("name") + ":发来post请求");
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("get请求已收到");
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String authname = request.getParameter("reg");
        Integer auth = 0;

        if(authname.equals("admin")) {
            auth = 1;
        } else if(authname.equals("student")) {
            auth = 2;
        }else if(authname.equals("teacher")) {
            auth = 3;
        }else if(authname.equals("bookseller")) {
            auth = 4;
        }

        int res = loginService.login(username, password, auth);
        response.setContentType("text/html;charset=utf-8");
        if(res == 1){
            HttpSession session = request.getSession();
            Manager manager = managerRepository.FindByAdminId(username);
            session.setAttribute("Authority", "1");
            session.setAttribute("Data", manager);
            response.sendRedirect("/admin/indexAdmin.jsp");
//            request.getRequestDispatcher("/admin/indexAdmin.jsp").forward(request, response);
        } else if(res == 2){
            HttpSession session = request.getSession();
            Student student = studentRepository.FindByStudentId(username);
            session.setAttribute("Authority", "2");
            session.setAttribute("Data", student);
            response.sendRedirect("/admin/indexStudent.jsp");
        } else if(res == 3) {
            HttpSession session = request.getSession();
            Teacher teacher = teacherRepository.FindByTeacherId(username);
            session.setAttribute("Authority", "3");
            session.setAttribute("Data", teacher);
            response.sendRedirect("/admin/indexTeacher.jsp");
        } else if(res == 4) {
            HttpSession session = request.getSession();
            BookSeller bookseller = bookSellerRepository.FindByBookSellerId(username);
            session.setAttribute("Authority", "4");
            session.setAttribute("Data", bookseller);
            response.sendRedirect("/admin/indexBookSeller.jsp");
        } else {
            response.setHeader("refresh", "3;URL=/login.jsp");
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<h3>您登录失败！3秒后返回</h2>");
        }



    }
}
